package DAO;

import java.util.Date;
import java.util.List;
import java.util.Vector;

import hibernate.AssetInfo;
import hibernate.UserLog;
import hibernateUtil.HibernateUtil;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class LogDAO {

	public int count = 0;
	public void addUserLog(String targetObject,String StartDate)
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		
		UserLog uL = new UserLog();
		
		//Date currentlyTime = java.sql.Date.valueOf(operateDate); 

		uL.setTargetObject(targetObject);
		uL.setStartDate(StartDate);
		uL.setBelongTo(" ");
		
		session.save(uL);
		trans.commit();
	}
	public void deleteLog(Vector operaterID)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 
		
		for(int i=0; i<operaterID.size(); i++ )
		{
			UserLog logInfo = (UserLog)session.get(UserLog.class, Integer.parseInt((String)operaterID.get(i)));
			session.delete(logInfo);
		}
		
		trans.commit();
    }	

	public List getAllLog(int start,int limit){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
				
		Query query = session.createQuery("from UserLog order by logID desc");
		count = query.list().size();
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List l = query.list();
		trans.commit();		
		return l;		
	};
    @SuppressWarnings("unchecked")

    public List getLogByQuery(int start,int limit,String targetObject)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		
		String str = "from UserLog where ";		
		if(!"".equals(targetObject))
		{
			str = str + "targetObject =:targetObject order by logID desc";
		}

		Query query = session.createQuery(str);  
		
		if(!"".equals(targetObject))
		{
			query.setString("targetObject", targetObject);
		}
		
		count = query.list().size();
		query.setFirstResult(start);  
		query.setMaxResults(limit); 	

		List l = query.list();
		trans.commit();
		
		return l;
    }
    
    
    public void updateUserLog(String targetObject,String EndDate)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		Query q = session.createQuery("update UserLog set EndDate=:EndDate where targetObject=:targetObject and EndDate is NULL");
		q.setString("EndDate",EndDate);
		q.setString("targetObject",targetObject);
		
		String temp = q.toString();
		
		System.out.println(temp);
		q.executeUpdate();
		
		trans.commit();		
    }
    
    public void updateBelongTo(String asset[])
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		Query q = session.createQuery("update UserLog set belongTo=:belongTo where targetObject=:targetObject and EndDate is NULL");
		q.setString("belongTo",asset[1]);
		q.setString("targetObject",asset[0]);
		
		String temp = q.toString();
		
		System.out.println(temp);
		q.executeUpdate();
		
		trans.commit();		
    }
    
}
